package com.weizhizs.assistant.code.vo;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class TableVO {

    public static final int TABLE_TYPE_NORMAL = 0;
    public static final int TABLE_TYPE_UPD = 1;
    public static final int TABLE_TYPE_DEL = 2;

    /**
     * 表的名称, sys_user
     */
    private String tableName;

    /**
     * 表的备注,  系统用户
     */
    private String tableComment;

    private int tableType;

    /**
     * 表的主键，   user_id....
     */
    private ColumnVO pk = new ColumnVO();

    /**
     * 表的列名(不包含主键)
     */
    private List<ColumnVO> columns = new ArrayList<>();

    /**
     * 类名(第一个字母小写)，如：sys_user => sysUser
     */
    private String className;

    private ColumnVO queryField = null;

    private List<String> enumNames = null;

    public List<ColumnVO> getAllColumns() {
        List<ColumnVO> allColumns = new ArrayList<>();
        allColumns.add(this.pk);
        allColumns.addAll(columns);
        return allColumns;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public String readTableComment() {
        if (null == tableComment) {
            return "";
        }
        String realStr = tableComment.trim();
        realStr = realStr.replace("，", ",");
        int pos = realStr.indexOf(",");
        String result = realStr.trim();
        if (pos > 0) {
            result = realStr.substring(0, pos).trim();
        }
//        System.out.print(result + ",");
        if (result.length() > 0 && "表".equals(result.substring(result.length() - 1))) {
            result = result.substring(0, result.length() - 1);
        }
//        System.out.println(result);
        return result;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public ColumnVO getPk() {
        return pk;
    }

    public void setPk(ColumnVO pk) {
        this.pk = pk;
    }

    public List<ColumnVO> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnVO> columns) {
        this.columns = columns;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public ColumnVO getQueryField() {
        return queryField;
    }

    public void setQueryField(ColumnVO queryField) {
        this.queryField = queryField;
    }

    public List<String> getEnumNames() {
        return enumNames;
    }

    public void setEnumNames(List<String> enumNames) {
        this.enumNames = enumNames;
    }

    public int getTableType() {
        return readTableType();
    }

    private int readTableType() {
        if (haveDelField()) {
            return TABLE_TYPE_DEL;
        } else if (haveUpdField()) {
            return TABLE_TYPE_UPD;
        } else {
            return TABLE_TYPE_NORMAL;
        }
    }

    private boolean haveDelField() {
        for(ColumnVO col : columns) {
            if ("del_flag".equals(col.getColumnName())) {
                return true;
            }
        }
        return false;
    }

    private boolean haveUpdField() {
        for(ColumnVO col : columns) {
            if ("upd_user".equals(col.getColumnName())) {
                return true;
            }
        }
        return false;
    }

    public boolean haveDisableFlagField() {
        for(ColumnVO col : columns) {
            if ("disable_flag".equals(col.getColumnName())) {
                return true;
            }
        }
        return false;
    }

}
